﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using GPSTrackingDBV2.Entities;
using GPSTrackingDBV2.Data;

#endregion

namespace GPSTrackingDBV2.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="MasterRolesProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class MasterRolesProviderBaseCore : EntityProviderBase<GPSTrackingDBV2.Entities.MasterRoles, GPSTrackingDBV2.Entities.MasterRolesKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByMasterUsernameFromMastersRolesByMaster
		
		/// <summary>
		///		Gets MasterRoles objects from the datasource by MasterUsername in the
		///		MastersRolesByMaster table. Table MasterRoles is related to table Masters
		///		through the (M:N) relationship defined in the MastersRolesByMaster table.
		/// </summary>
		/// <param name="_masterUsername"></param>
		/// <returns>Returns a typed collection of MasterRoles objects.</returns>
		public TList<MasterRoles> GetByMasterUsernameFromMastersRolesByMaster(System.String _masterUsername)
		{
			int count = -1;
			return GetByMasterUsernameFromMastersRolesByMaster(null,_masterUsername, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets GPSTrackingDBV2.Entities.MasterRoles objects from the datasource by MasterUsername in the
		///		MastersRolesByMaster table. Table MasterRoles is related to table Masters
		///		through the (M:N) relationship defined in the MastersRolesByMaster table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_masterUsername"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of MasterRoles objects.</returns>
		public TList<MasterRoles> GetByMasterUsernameFromMastersRolesByMaster(System.String _masterUsername, int start, int pageLength)
		{
			int count = -1;
			return GetByMasterUsernameFromMastersRolesByMaster(null, _masterUsername, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets MasterRoles objects from the datasource by MasterUsername in the
		///		MastersRolesByMaster table. Table MasterRoles is related to table Masters
		///		through the (M:N) relationship defined in the MastersRolesByMaster table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_masterUsername"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of MasterRoles objects.</returns>
		public TList<MasterRoles> GetByMasterUsernameFromMastersRolesByMaster(TransactionManager transactionManager, System.String _masterUsername)
		{
			int count = -1;
			return GetByMasterUsernameFromMastersRolesByMaster(transactionManager, _masterUsername, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets MasterRoles objects from the datasource by MasterUsername in the
		///		MastersRolesByMaster table. Table MasterRoles is related to table Masters
		///		through the (M:N) relationship defined in the MastersRolesByMaster table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_masterUsername"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of MasterRoles objects.</returns>
		public TList<MasterRoles> GetByMasterUsernameFromMastersRolesByMaster(TransactionManager transactionManager, System.String _masterUsername,int start, int pageLength)
		{
			int count = -1;
			return GetByMasterUsernameFromMastersRolesByMaster(transactionManager, _masterUsername, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets MasterRoles objects from the datasource by MasterUsername in the
		///		MastersRolesByMaster table. Table MasterRoles is related to table Masters
		///		through the (M:N) relationship defined in the MastersRolesByMaster table.
		/// </summary>
		/// <param name="_masterUsername"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of MasterRoles objects.</returns>
		public TList<MasterRoles> GetByMasterUsernameFromMastersRolesByMaster(System.String _masterUsername,int start, int pageLength, out int count)
		{
			
			return GetByMasterUsernameFromMastersRolesByMaster(null, _masterUsername, start, pageLength, out count);
		}


		/// <summary>
		///		Gets MasterRoles objects from the datasource by MasterUsername in the
		///		MastersRolesByMaster table. Table MasterRoles is related to table Masters
		///		through the (M:N) relationship defined in the MastersRolesByMaster table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_masterUsername"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of MasterRoles objects.</returns>
		public abstract TList<MasterRoles> GetByMasterUsernameFromMastersRolesByMaster(TransactionManager transactionManager,System.String _masterUsername, int start, int pageLength, out int count);
		
		#endregion GetByMasterUsernameFromMastersRolesByMaster
		
		#region GetByMenuIdFromMenuByMasterRoles
		
		/// <summary>
		///		Gets MasterRoles objects from the datasource by MenuID in the
		///		MenuByMasterRoles table. Table MasterRoles is related to table Menu
		///		through the (M:N) relationship defined in the MenuByMasterRoles table.
		/// </summary>
		/// <param name="_menuId"></param>
		/// <returns>Returns a typed collection of MasterRoles objects.</returns>
		public TList<MasterRoles> GetByMenuIdFromMenuByMasterRoles(System.Int32 _menuId)
		{
			int count = -1;
			return GetByMenuIdFromMenuByMasterRoles(null,_menuId, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets GPSTrackingDBV2.Entities.MasterRoles objects from the datasource by MenuID in the
		///		MenuByMasterRoles table. Table MasterRoles is related to table Menu
		///		through the (M:N) relationship defined in the MenuByMasterRoles table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_menuId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of MasterRoles objects.</returns>
		public TList<MasterRoles> GetByMenuIdFromMenuByMasterRoles(System.Int32 _menuId, int start, int pageLength)
		{
			int count = -1;
			return GetByMenuIdFromMenuByMasterRoles(null, _menuId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets MasterRoles objects from the datasource by MenuID in the
		///		MenuByMasterRoles table. Table MasterRoles is related to table Menu
		///		through the (M:N) relationship defined in the MenuByMasterRoles table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_menuId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of MasterRoles objects.</returns>
		public TList<MasterRoles> GetByMenuIdFromMenuByMasterRoles(TransactionManager transactionManager, System.Int32 _menuId)
		{
			int count = -1;
			return GetByMenuIdFromMenuByMasterRoles(transactionManager, _menuId, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets MasterRoles objects from the datasource by MenuID in the
		///		MenuByMasterRoles table. Table MasterRoles is related to table Menu
		///		through the (M:N) relationship defined in the MenuByMasterRoles table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_menuId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of MasterRoles objects.</returns>
		public TList<MasterRoles> GetByMenuIdFromMenuByMasterRoles(TransactionManager transactionManager, System.Int32 _menuId,int start, int pageLength)
		{
			int count = -1;
			return GetByMenuIdFromMenuByMasterRoles(transactionManager, _menuId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets MasterRoles objects from the datasource by MenuID in the
		///		MenuByMasterRoles table. Table MasterRoles is related to table Menu
		///		through the (M:N) relationship defined in the MenuByMasterRoles table.
		/// </summary>
		/// <param name="_menuId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of MasterRoles objects.</returns>
		public TList<MasterRoles> GetByMenuIdFromMenuByMasterRoles(System.Int32 _menuId,int start, int pageLength, out int count)
		{
			
			return GetByMenuIdFromMenuByMasterRoles(null, _menuId, start, pageLength, out count);
		}


		/// <summary>
		///		Gets MasterRoles objects from the datasource by MenuID in the
		///		MenuByMasterRoles table. Table MasterRoles is related to table Menu
		///		through the (M:N) relationship defined in the MenuByMasterRoles table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_menuId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of MasterRoles objects.</returns>
		public abstract TList<MasterRoles> GetByMenuIdFromMenuByMasterRoles(TransactionManager transactionManager,System.Int32 _menuId, int start, int pageLength, out int count);
		
		#endregion GetByMenuIdFromMenuByMasterRoles
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, GPSTrackingDBV2.Entities.MasterRolesKey key)
		{
			return Delete(transactionManager, key.MasterRoleId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_masterRoleId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _masterRoleId)
		{
			return Delete(null, _masterRoleId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_masterRoleId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _masterRoleId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override GPSTrackingDBV2.Entities.MasterRoles Get(TransactionManager transactionManager, GPSTrackingDBV2.Entities.MasterRolesKey key, int start, int pageLength)
		{
			return GetByMasterRoleId(transactionManager, key.MasterRoleId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_MasterRoles index.
		/// </summary>
		/// <param name="_masterRoleId"></param>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.MasterRoles"/> class.</returns>
		public GPSTrackingDBV2.Entities.MasterRoles GetByMasterRoleId(System.Int32 _masterRoleId)
		{
			int count = -1;
			return GetByMasterRoleId(null,_masterRoleId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_MasterRoles index.
		/// </summary>
		/// <param name="_masterRoleId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.MasterRoles"/> class.</returns>
		public GPSTrackingDBV2.Entities.MasterRoles GetByMasterRoleId(System.Int32 _masterRoleId, int start, int pageLength)
		{
			int count = -1;
			return GetByMasterRoleId(null, _masterRoleId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_MasterRoles index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_masterRoleId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.MasterRoles"/> class.</returns>
		public GPSTrackingDBV2.Entities.MasterRoles GetByMasterRoleId(TransactionManager transactionManager, System.Int32 _masterRoleId)
		{
			int count = -1;
			return GetByMasterRoleId(transactionManager, _masterRoleId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_MasterRoles index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_masterRoleId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.MasterRoles"/> class.</returns>
		public GPSTrackingDBV2.Entities.MasterRoles GetByMasterRoleId(TransactionManager transactionManager, System.Int32 _masterRoleId, int start, int pageLength)
		{
			int count = -1;
			return GetByMasterRoleId(transactionManager, _masterRoleId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_MasterRoles index.
		/// </summary>
		/// <param name="_masterRoleId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.MasterRoles"/> class.</returns>
		public GPSTrackingDBV2.Entities.MasterRoles GetByMasterRoleId(System.Int32 _masterRoleId, int start, int pageLength, out int count)
		{
			return GetByMasterRoleId(null, _masterRoleId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_MasterRoles index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_masterRoleId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.MasterRoles"/> class.</returns>
		public abstract GPSTrackingDBV2.Entities.MasterRoles GetByMasterRoleId(TransactionManager transactionManager, System.Int32 _masterRoleId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;MasterRoles&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;MasterRoles&gt;"/></returns>
		public static TList<MasterRoles> Fill(IDataReader reader, TList<MasterRoles> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				GPSTrackingDBV2.Entities.MasterRoles c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("MasterRoles")
					.Append("|").Append((System.Int32)reader[((int)MasterRolesColumn.MasterRoleId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<MasterRoles>(
					key.ToString(), // EntityTrackingKey
					"MasterRoles",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new GPSTrackingDBV2.Entities.MasterRoles();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.MasterRoleId = (System.Int32)reader[((int)MasterRolesColumn.MasterRoleId - 1)];
					c.OriginalMasterRoleId = c.MasterRoleId;
					c.Name = (System.String)reader[((int)MasterRolesColumn.Name - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="GPSTrackingDBV2.Entities.MasterRoles"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.MasterRoles"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, GPSTrackingDBV2.Entities.MasterRoles entity)
		{
			if (!reader.Read()) return;
			
			entity.MasterRoleId = (System.Int32)reader[((int)MasterRolesColumn.MasterRoleId - 1)];
			entity.OriginalMasterRoleId = (System.Int32)reader["MasterRoleID"];
			entity.Name = (System.String)reader[((int)MasterRolesColumn.Name - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="GPSTrackingDBV2.Entities.MasterRoles"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.MasterRoles"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, GPSTrackingDBV2.Entities.MasterRoles entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.MasterRoleId = (System.Int32)dataRow["MasterRoleID"];
			entity.OriginalMasterRoleId = (System.Int32)dataRow["MasterRoleID"];
			entity.Name = (System.String)dataRow["Name"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.MasterRoles"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">GPSTrackingDBV2.Entities.MasterRoles Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, GPSTrackingDBV2.Entities.MasterRoles entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByMasterRoleId methods when available
			
			#region MenuByMasterRolesCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<MenuByMasterRoles>|MenuByMasterRolesCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'MenuByMasterRolesCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.MenuByMasterRolesCollection = DataRepository.MenuByMasterRolesProvider.GetByMasterRoleId(transactionManager, entity.MasterRoleId);

				if (deep && entity.MenuByMasterRolesCollection.Count > 0)
				{
					deepHandles.Add("MenuByMasterRolesCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<MenuByMasterRoles>) DataRepository.MenuByMasterRolesProvider.DeepLoad,
						new object[] { transactionManager, entity.MenuByMasterRolesCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region MenuIdMenuCollection_From_MenuByMasterRoles
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<Menu>|MenuIdMenuCollection_From_MenuByMasterRoles", deepLoadType, innerList))
			{
				entity.MenuIdMenuCollection_From_MenuByMasterRoles = DataRepository.MenuProvider.GetByMasterRoleIdFromMenuByMasterRoles(transactionManager, entity.MasterRoleId);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'MenuIdMenuCollection_From_MenuByMasterRoles' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.MenuIdMenuCollection_From_MenuByMasterRoles != null)
				{
					deepHandles.Add("MenuIdMenuCollection_From_MenuByMasterRoles",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< Menu >) DataRepository.MenuProvider.DeepLoad,
						new object[] { transactionManager, entity.MenuIdMenuCollection_From_MenuByMasterRoles, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region MasterUsernameMastersCollection_From_MastersRolesByMaster
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<Masters>|MasterUsernameMastersCollection_From_MastersRolesByMaster", deepLoadType, innerList))
			{
				entity.MasterUsernameMastersCollection_From_MastersRolesByMaster = DataRepository.MastersProvider.GetByMasterRoleIdFromMastersRolesByMaster(transactionManager, entity.MasterRoleId);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'MasterUsernameMastersCollection_From_MastersRolesByMaster' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.MasterUsernameMastersCollection_From_MastersRolesByMaster != null)
				{
					deepHandles.Add("MasterUsernameMastersCollection_From_MastersRolesByMaster",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< Masters >) DataRepository.MastersProvider.DeepLoad,
						new object[] { transactionManager, entity.MasterUsernameMastersCollection_From_MastersRolesByMaster, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region MastersRolesByMasterCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<MastersRolesByMaster>|MastersRolesByMasterCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'MastersRolesByMasterCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.MastersRolesByMasterCollection = DataRepository.MastersRolesByMasterProvider.GetByMasterRoleId(transactionManager, entity.MasterRoleId);

				if (deep && entity.MastersRolesByMasterCollection.Count > 0)
				{
					deepHandles.Add("MastersRolesByMasterCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<MastersRolesByMaster>) DataRepository.MastersRolesByMasterProvider.DeepLoad,
						new object[] { transactionManager, entity.MastersRolesByMasterCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the GPSTrackingDBV2.Entities.MasterRoles object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">GPSTrackingDBV2.Entities.MasterRoles instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">GPSTrackingDBV2.Entities.MasterRoles Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, GPSTrackingDBV2.Entities.MasterRoles entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region MenuIdMenuCollection_From_MenuByMasterRoles>
			if (CanDeepSave(entity.MenuIdMenuCollection_From_MenuByMasterRoles, "List<Menu>|MenuIdMenuCollection_From_MenuByMasterRoles", deepSaveType, innerList))
			{
				if (entity.MenuIdMenuCollection_From_MenuByMasterRoles.Count > 0 || entity.MenuIdMenuCollection_From_MenuByMasterRoles.DeletedItems.Count > 0)
				{
					DataRepository.MenuProvider.Save(transactionManager, entity.MenuIdMenuCollection_From_MenuByMasterRoles); 
					deepHandles.Add("MenuIdMenuCollection_From_MenuByMasterRoles",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<Menu>) DataRepository.MenuProvider.DeepSave,
						new object[] { transactionManager, entity.MenuIdMenuCollection_From_MenuByMasterRoles, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 

			#region MasterUsernameMastersCollection_From_MastersRolesByMaster>
			if (CanDeepSave(entity.MasterUsernameMastersCollection_From_MastersRolesByMaster, "List<Masters>|MasterUsernameMastersCollection_From_MastersRolesByMaster", deepSaveType, innerList))
			{
				if (entity.MasterUsernameMastersCollection_From_MastersRolesByMaster.Count > 0 || entity.MasterUsernameMastersCollection_From_MastersRolesByMaster.DeletedItems.Count > 0)
				{
					DataRepository.MastersProvider.Save(transactionManager, entity.MasterUsernameMastersCollection_From_MastersRolesByMaster); 
					deepHandles.Add("MasterUsernameMastersCollection_From_MastersRolesByMaster",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<Masters>) DataRepository.MastersProvider.DeepSave,
						new object[] { transactionManager, entity.MasterUsernameMastersCollection_From_MastersRolesByMaster, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<MenuByMasterRoles>
				if (CanDeepSave(entity.MenuByMasterRolesCollection, "List<MenuByMasterRoles>|MenuByMasterRolesCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(MenuByMasterRoles child in entity.MenuByMasterRolesCollection)
					{
						if(child.MasterRoleIdSource != null)
						{
								child.MasterRoleId = child.MasterRoleIdSource.MasterRoleId;
						}

						if(child.MenuIdSource != null)
						{
								child.MenuId = child.MenuIdSource.MenuId;
						}

					}

					if (entity.MenuByMasterRolesCollection.Count > 0 || entity.MenuByMasterRolesCollection.DeletedItems.Count > 0)
					{
						//DataRepository.MenuByMasterRolesProvider.Save(transactionManager, entity.MenuByMasterRolesCollection);
						
						deepHandles.Add("MenuByMasterRolesCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< MenuByMasterRoles >) DataRepository.MenuByMasterRolesProvider.DeepSave,
							new object[] { transactionManager, entity.MenuByMasterRolesCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<MastersRolesByMaster>
				if (CanDeepSave(entity.MastersRolesByMasterCollection, "List<MastersRolesByMaster>|MastersRolesByMasterCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(MastersRolesByMaster child in entity.MastersRolesByMasterCollection)
					{
						if(child.MasterRoleIdSource != null)
						{
								child.MasterRoleId = child.MasterRoleIdSource.MasterRoleId;
						}

						if(child.MasterUsernameSource != null)
						{
								child.MasterUsername = child.MasterUsernameSource.MasterUsername;
						}

					}

					if (entity.MastersRolesByMasterCollection.Count > 0 || entity.MastersRolesByMasterCollection.DeletedItems.Count > 0)
					{
						//DataRepository.MastersRolesByMasterProvider.Save(transactionManager, entity.MastersRolesByMasterCollection);
						
						deepHandles.Add("MastersRolesByMasterCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< MastersRolesByMaster >) DataRepository.MastersRolesByMasterProvider.DeepSave,
							new object[] { transactionManager, entity.MastersRolesByMasterCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region MasterRolesChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>GPSTrackingDBV2.Entities.MasterRoles</c>
	///</summary>
	public enum MasterRolesChildEntityTypes
	{

		///<summary>
		/// Collection of <c>MasterRoles</c> as OneToMany for MenuByMasterRolesCollection
		///</summary>
		[ChildEntityType(typeof(TList<MenuByMasterRoles>))]
		MenuByMasterRolesCollection,

		///<summary>
		/// Collection of <c>MasterRoles</c> as ManyToMany for MenuCollection_From_MenuByMasterRoles
		///</summary>
		[ChildEntityType(typeof(TList<Menu>))]
		MenuIdMenuCollection_From_MenuByMasterRoles,

		///<summary>
		/// Collection of <c>MasterRoles</c> as ManyToMany for MastersCollection_From_MastersRolesByMaster
		///</summary>
		[ChildEntityType(typeof(TList<Masters>))]
		MasterUsernameMastersCollection_From_MastersRolesByMaster,

		///<summary>
		/// Collection of <c>MasterRoles</c> as OneToMany for MastersRolesByMasterCollection
		///</summary>
		[ChildEntityType(typeof(TList<MastersRolesByMaster>))]
		MastersRolesByMasterCollection,
	}
	
	#endregion MasterRolesChildEntityTypes
	
	#region MasterRolesFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;MasterRolesColumn&gt;"/> class
	/// that is used exclusively with a <see cref="MasterRoles"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class MasterRolesFilterBuilder : SqlFilterBuilder<MasterRolesColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the MasterRolesFilterBuilder class.
		/// </summary>
		public MasterRolesFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the MasterRolesFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public MasterRolesFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the MasterRolesFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public MasterRolesFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion MasterRolesFilterBuilder
	
	#region MasterRolesParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;MasterRolesColumn&gt;"/> class
	/// that is used exclusively with a <see cref="MasterRoles"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class MasterRolesParameterBuilder : ParameterizedSqlFilterBuilder<MasterRolesColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the MasterRolesParameterBuilder class.
		/// </summary>
		public MasterRolesParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the MasterRolesParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public MasterRolesParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the MasterRolesParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public MasterRolesParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion MasterRolesParameterBuilder
	
	#region MasterRolesSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;MasterRolesColumn&gt;"/> class
	/// that is used exclusively with a <see cref="MasterRoles"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class MasterRolesSortBuilder : SqlSortBuilder<MasterRolesColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the MasterRolesSqlSortBuilder class.
		/// </summary>
		public MasterRolesSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion MasterRolesSortBuilder
	
} // end namespace
